<template>
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input v-model="form.licensePlate" placeholder="请输入车牌号" disabled />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input v-model="form.brand" placeholder="请输入车牌号" disabled />
      </el-form-item>
      <el-form-item label="购车年份" prop="purchaseYear">
        <el-date-picker clearable
          v-model="form.purchaseYear"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择购车年份"
          disabled>
        </el-date-picker>
      </el-form-item>
      <el-form-item label="车辆类型" prop="typeId">
        <el-input v-model="form.type" placeholder="请输车辆类型" disabled />
      </el-form-item>
      <el-form-item label="座位数" prop="seatCount">
        <el-input v-model="form.seatCount" placeholder="请输入座位数" disabled />
      </el-form-item>
      <el-form-item label="驾驶员" prop="driver">
        <el-input v-model="form.driver" placeholder="请输入驾驶员姓名"/>
      </el-form-item>
      <el-form-item label="车辆图片" prop="image">
        <image-upload v-model="form.image" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "EditVehicleDialog",
  props: {
    open: {
      type: Boolean,
      default: false
    },
    delVehicles: {
      type: Array,
      default: () => []
    },
    delVehiclesType: {
      type: Array,
      default: () => []
    },
    vehicleData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      title: "修改车辆",
      form: {
        id: null,
        licensePlate: null,
        brand: null,
        purchaseYear: null,
        type: null,
        image: null,
        seatCount: null,
        driver: null // 添加驾驶员字段
      },
      rules: {
      }
    };
  },
  watch: {
    vehicleData: {
      handler(newVal) {
        if (newVal && newVal.id) {
          this.form = {
            id: newVal.id,
            licensePlate: newVal.licensePlate,
            brand: newVal.brand,
            purchaseYear: newVal.purchaseYear,
            type: newVal.type,
            image: newVal.image,
            seatCount: newVal.seatCount,
            driver: newVal.driver // 初始化驾驶员字段
          };
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    // 取消按钮
    cancel() {
      this.$emit("update:open", false);
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        licensePlate: null,
        brandId: null,
        purchaseYear: null,
        typeId: null,
        image: null,
        seatCount: null,
        driver: null // 重置驾驶员字段
      };
      this.$refs["form"].resetFields();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const VehicleBrand = this.delVehicles.find(delVehicles => delVehicles.dictCode === this.form.brandId);
          if (VehicleBrand) {
            this.form.brand = VehicleBrand.dictLabel;
          } else {
            this.$modal.msgError("请选择正确的车辆品牌");
            return;
          }
          const VehicleType = this.delVehiclesType.find(delVehiclesType => delVehiclesType.dictCode === this.form.typeId);
          if (VehicleType) {
            this.form.type = VehicleType.dictLabel;
          } else {
            this.$modal.msgError("请选择车辆类型");
            return;
          }
          this.$emit("submit", this.form);
          this.$emit("update:open", false);
          this.reset();
        }
      });
    }
  }
};
</script>
